<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>图像填充</title>
  </head>
  <body>
    <script>
      const canvas = document.createElement("canvas");
      canvas.width = 30;
      canvas.height = 30;
      document.body.append(canvas);

      const ctx = canvas.getContext("2d");

      ctx.moveTo(0, 15);
      ctx.lineTo(15, 0);
      ctx.lineTo(30, 15);
      ctx.lineTo(15, 30);
      ctx.closePath();
      ctx.fill();

      const canvas1 = document.createElement("canvas");
      canvas1.width = 400;
      canvas1.height = 400;
      document.body.append(canvas1);

      const ctx1 = canvas1.getContext("2d");

      const img = new Image();
      img.src = "./背景.png";
      img.onload = function () {
        const pattern = ctx1.createPattern(img, "repeat");
        const pattern2 = ctx1.createPattern(canvas, "");

        ctx1.lineWidth = 30;
        ctx1.strokeStyle = pattern2;
        ctx1.fillStyle = pattern;
        ctx1.rect(15, 15, 330, 330);
        ctx1.stroke();
        ctx1.fill();
      };
    </script>
  </body>
</html>
